//
//  QZHomeMenceVC.m
//  shortvideoplay
//
//  Created by tikeios_002 on 2024/6/13.
//

#import "QZHomeMenceVC.h"
#import "YSYHasNetwork.h"
#import "JXCategoryTitleView.h"
#import "JXCategoryView.h"
#import "QZHomePlayVC.h"
#import "QZHomeRankVC.h"
#import "QZSeeUserPrivacyView.h"
#import "SPAlertController.h"
#import "AgreementViewController.h"
#import "YBGoodWorking.h"
#import "QZHomeFollowVC.h"
#import "QZTabbarController.h"
#import "QZHomeShortVC.h"

@interface QZHomeMenceVC ()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate,JXCategoryListContentViewDelegate>
@property(strong, nonatomic)NSArray * listArr;
@property(strong, nonatomic)NSMutableArray * nameArr;
@property (nonatomic, strong) JXCategoryTitleView *myCategoryView;
@property (nonatomic, strong) JXCategoryListContainerView * listContainerView;
@end

@implementation QZHomeMenceVC

-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.mm_drawerController setEnableGesture:NO];
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.nameArr addObject:@"推荐"];
    [self.nameArr addObject:@"短剧"];
    [self.nameArr addObject:@"动态"];
    [self.nameArr addObject:@"排行"];
    
    [self startNetworkingMethod];
}
- (void)startNetworkingMethod{
    KWeakSelf;
    [YSYHasNetwork ysy_hasNetwork:^(bool has) {
        if(has){
            // 工具栏配置
            if([QZUserInfoModel share].app_common_config == nil){
                [SVProgressHUDTool showWithStatus:nil];
                [weakSelf initAPP];
            }
            
            
        }else{
            [SVProgressHUDTool dismiss];
//            [weakSelf.tabBarController.view addNoNetWorkingEmpty:^{
//                [SVProgressHUDTool showWithStatus:nil];
//                [weakSelf requestDatas];
//            }];
//            [weakSelf.tabBarController.view setBlackTheme];
        }
    }];
}
#pragma mark -初始化
- (NSMutableArray *)nameArr{
    if(_nameArr == nil){
        _nameArr = [NSMutableArray new];
    }
    return _nameArr;
}
#pragma mark -网络区域
- (void)initAPP{
    KMutableDic(params)
    KWeakSelf
    [YBNetBase initAppRequest:params successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        [SVProgressHUDTool dismiss];
        if(code == 200){
            [QZUserInfoModel share].app_common_config = data[@"app_common_config"];
            [QZUserInfoModel share].invite_switch = data[@"app_common_config"][@"invite_switch"];
            [QZUserInfoModel share].creator_refund_switch = data[@"app_common_config"][@"creator_refund_switch"];
            if(data[@"update"] != nil){
                [QZUserInfoModel share].updata = data[@"update"];
                [[BSystemAlert shared] showAlertWithTitle:[QZUserInfoModel share].updata[0][@"titles"] message:[QZUserInfoModel share].updata[0][@"contents"] userData:nil cancelTitle:NSLocalizedString(@"取消", nil) actions:@[NSLocalizedString(@"更新", nil)] actionBlock:^(id  _Nonnull userData, NSString * _Nonnull actionTitle) {
                    if ([actionTitle isEqualToString:NSLocalizedString(@"更新", nil)]) {
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:SHAREURL] options:@{} completionHandler:^(BOOL success) {
                            
                        }];
                    }
                }];
            }
            if (data[@"app_common_config"] != nil) {
                [QZUserInfoModel share].pri = data[@"app_common_config"][@"url_privacy_policy"];
                [QZUserInfoModel share].logout = data[@"app_common_config"][@"url_account_logout"];
                [QZUserInfoModel share].agr = data[@"app_common_config"][@"url_user_agreement"];
                [QZUserInfoModel share].cre = data[@"app_common_config"][@"creattor_agreement"];
                [QZUserInfoModel share].cha = data[@"app_common_config"][@"url_t_coin_recharge"];
                [QZUserInfoModel share].buy = data[@"app_common_config"][@"url_buy_vip"];
//                [weakSelf pageRequest];
                [weakSelf addTitleView];
                [weakSelf goodRequest];
                if([QZUserInfoModel isLogin]){
                    [weakSelf jifengRequest];
                }
            }
            [(QZTabbarController *)self.tabBarController getCreatorMoneyChangeRequet];
        }else{
            [SVProgressHUDTool dismiss];
            [YBMsgPop showPop:msg];
        }
    }];
}
- (void)goodRequest{
//    KMutableDic(sendDic)
//    sendDic[@"page"] = @0;
//    sendDic[@"page_size"] = @15;
//    [YBGoodWorking postWithUrl:@"product/search" Dic:sendDic Suc:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
//        
//    }];
}
- (void)jifengRequest{
    KMutableDic(sendDic)
    [YBNetBase activityTaskWatchVideoInfoRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            [QZHongBaoView share].max_time = [data[@"time_length"][@"max"] intValue];
            [QZHongBaoView share].min_time = [data[@"time_length"][@"min"] intValue];
            NSLog(@"%@",data);
        }
    }];
}
- (void)pageRequest{
    [SVProgressHUDTool showWithStatus:nil];
    KMutableDic(params);
    KWeakSelf
    [YBNetBase videoTypeListRequest:params successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            weakSelf.listArr = data[@"ext_list"];
            for (NSDictionary * dic in weakSelf.listArr) {
                [weakSelf.nameArr addObject:dic[@"name"]];
            }
            [self addTitleView];
            [SVProgressHUDTool dismiss];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
- (void)addTitleView{
    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"userSeeXiYi"] intValue] == 0){
        
        QZSeeUserPrivacyView * see = [[QZSeeUserPrivacyView alloc]init];
        see.frame = CGRectMake(0, 0, 330, 345);
        
        SPAlertController * alertVC = [SPAlertController alertControllerWithCustomAlertView:see  preferredStyle:SPAlertControllerStyleAlert animationType:SPAlertAnimationTypeDefault];
        alertVC.minDistanceToEdges = (KScreen_W-330)/2;
//        [alertVC updateCustomViewSize:CGSizeMake(330, 185)];
        alertVC.tapBackgroundViewDismiss = NO;
        [self presentViewController:alertVC animated:YES completion:nil];
        KWeakSelf
        see.seeUserPrivacyBlock = ^(NSInteger index) {
            if(index == 0){
                AgreementViewController * xy = [[AgreementViewController alloc]init];
                xy.modalPresentationStyle = UIModalPresentationFullScreen;
                xy.url = [QZUserInfoModel share].agr; //@"https://docs.qq.com/doc/p/19945cd532bc0d9a3b8882b46d78c25c7c624562";
                xy.Nav_title = @"用户协议";
                xy.isPerSent = YES;
                [alertVC presentViewController:xy animated:YES completion:nil];
            }else if(index == 1){
                AgreementViewController * xy = [[AgreementViewController alloc]init];
                xy.modalPresentationStyle = UIModalPresentationFullScreen;
                xy.url = [QZUserInfoModel share].pri; //@"https://docs.qq.com/doc/p/92365bb913de46bbfabc7cd51656e9cce6f4e4a9";
                xy.Nav_title = @"隐私政策";
                xy.isPerSent = YES;
                [alertVC presentViewController:xy animated:YES completion:nil];
            }else if(index == 2){
                exit(0);
            }else if(index == 3){
                [[NSUserDefaults standardUserDefaults] setValue:@(1) forKey:@"userSeeXiYi"];
                [alertVC dismissViewControllerAnimated:YES completion:nil];
                [weakSelf addTitleView];
            }
        };
        return;
    }
    self.myCategoryView = [[JXCategoryTitleView alloc] init];
    self.myCategoryView.backgroundColor = [UIColor clearColor];
    self.myCategoryView.frame = CGRectMake(0, kStatusBarHeight+2, KScreen_W, 40);
    self.myCategoryView.titleFont = FontMedium(14);
    self.myCategoryView.titleSelectedFont = FontBold(16);
    self.myCategoryView.titles = self.nameArr;
    self.myCategoryView.titleColorGradientEnabled = YES;
    self.myCategoryView.titleColor = COLOR_ALPH(255, 255, 255, 0.77);
    self.myCategoryView.titleSelectedColor = [UIColor whiteColor];
    self.myCategoryView.delegate = self;
    
//    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
//    lineView.indicatorColor = [UIColor whiteColor];
//    lineView.indicatorWidth = 30.0f;
//    lineView.indicatorCornerRadius = 0;
//    lineView.lineStyle = JXCategoryIndicatorLineStyle_Normal;
//    self.myCategoryView.indicators = @[lineView];
//    JXCategoryIndicatorImageView *indicatorImageView = [[JXCategoryIndicatorImageView alloc] init];
//    indicatorImageView.indicatorImageViewRollEnabled = NO;
//
//    indicatorImageView.indicatorImageView.image = [UIImage imageNamed:@"huxian"];
//    self.myCategoryView.indicators = @[indicatorImageView];
    
    self.listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    self.listContainerView.bounces = YES;
    self.listContainerView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    self.listContainerView.frame = CGRectMake(0, 0, KScreen_W, KScreen_H-KTab_H);
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:self.listContainerView];
       UILabel * oldshowSSSSTip2 = [[UILabel alloc]init];
    oldshowSSSSTip2.clipsToBounds = YES;
    oldshowSSSSTip2.text = @"kass显示区";
    oldshowSSSSTip2.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip2.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip2.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip2.hidden = YES;
    [self.view addSubview:oldshowSSSSTip2];
    [self.view addSubview:self.myCategoryView];
    self.myCategoryView.contentScrollView = self.listContainerView.scrollView;
    self.myCategoryView.listContainer = self.listContainerView;
     
}

- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.nameArr.count;
}
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    if(index == 0){
        QZHomePlayVC *play = [[QZHomePlayVC alloc] init];
        return play;
    }else if(index == 1){
        QZHomeShortVC *rank = [[QZHomeShortVC alloc] init];
        return rank;
    }else if(index == 2){
        QZHomeFollowVC *rank = [[QZHomeFollowVC alloc] init];
        return rank;
    }else{
        QZHomeRankVC *rank = [[QZHomeRankVC alloc] init];
        return rank;
    }
}
- (BOOL)categoryView:(JXCategoryBaseView *)categoryView canClickItemAtIndex:(NSInteger)index{
    if([QZUserInfoModel isLogin] == NO && index > 0){
        [QZUserInfoModel presentLoginView];
        return NO;
    }else{
        return YES;
    }
}
- (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio{
    if(leftIndex == 0 && [QZUserInfoModel isLogin] == NO){
        categoryView.contentScrollView.contentSize  = CGSizeMake(0, categoryView.contentScrollView.contentSize.height);
    }
}
@end
